package com.blast.comicsisland.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.blast.comicsisland.R;
import com.blast.comicsisland.adapters.BookstoreAdapter;

import java.util.LinkedList;
import java.util.List;


/**
 * 书城Fragment 上方为RadioButton
 * 推荐 榜单（忽略内容） 游戏（忽略全部） 专题
 * 下方为ViewPager
 */
public class BookStoreFragment extends Fragment implements ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener {

    private RadioGroup group;
    private ViewPager pager;
    private RecommendFragment recommendFragment;
    private RankFragment rankFragment;
    private GameFragment gameFragment;
    private TopicFragment topicFragment;
    private List<Fragment> fragments;

    public BookStoreFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_book_store, container, false);

        group = (RadioGroup) view.findViewById(R.id.bookstore_radio_group);
        pager = (ViewPager) view.findViewById(R.id.bookstore_view_pager);
        pager.setOffscreenPageLimit(4);
        fragments = new LinkedList<Fragment>();
        recommendFragment = new RecommendFragment();
        rankFragment = new RankFragment();
        gameFragment = new GameFragment();
        topicFragment = new TopicFragment();

        View childOne = group.getChildAt(0);
        if (childOne != null) {
            if (childOne instanceof RadioButton) {
                RadioButton btnRecommend = (RadioButton) childOne;
                btnRecommend.setChecked(true);
            }
        }

        fragments.add(recommendFragment);
        fragments.add(rankFragment);
        fragments.add(gameFragment);
        fragments.add(topicFragment);


        BookstoreAdapter adapter = new BookstoreAdapter(getChildFragmentManager(),fragments);

        pager.setAdapter(adapter);

        pager.setOnPageChangeListener(this);
        group.setOnCheckedChangeListener(this);

        return view;
    }


    @Override
    public void onDetach() {
        super.onDetach();
    }

    //-------------------------------------------->
    //ViewPager 滑动事件
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        View child = group.getChildAt(position*2);
        if (child instanceof RadioButton && child != null) {
            RadioButton radioButton = (RadioButton) child;
            radioButton.setChecked(true);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    //---------------------------------------------->
    //group点击事件
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int positionChecked = 0;
        int childCount = group.getChildCount();
        if (childCount> 0){
            for (int i = 0; i < childCount; i++) {
                View child = group.getChildAt(i);
                if (child instanceof RadioButton){
                    RadioButton radioButton = (RadioButton) child;

                    if (radioButton.isChecked()){
                        positionChecked = i/2;
                        break;
                    }
                }
            }

        }
        int count = pager.getChildCount();
        if (count>=positionChecked) {
            pager.setCurrentItem(positionChecked);
        }
    }




}
